package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import io.lettuce.core.StrAlgoArgs;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ByStandardSystemMapper;
import com.ruoyi.system.domain.ByStandardSystem;
import com.ruoyi.system.service.IByStandardSystemService;

/**
 * 体系Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-12
 */
@Service
public class ByStandardSystemServiceImpl implements IByStandardSystemService 
{
    @Autowired
    private ByStandardSystemMapper byStandardSystemMapper;

    /**
     * 查询体系
     * 
     * @param id 体系主键
     * @return 体系
     */
    @Override
    public ByStandardSystem selectByStandardSystemById(Long id)
    {
        return byStandardSystemMapper.selectByStandardSystemById(id);
    }

    /**
     * 查询体系列表
     * 
     * @param byStandardSystem 体系
     * @return 体系
     */
    @Override
    public List<ByStandardSystem> selectByStandardSystemList(ByStandardSystem byStandardSystem)
    {
        return byStandardSystemMapper.selectByStandardSystemList(byStandardSystem);
    }

    /**
     * 新增体系
     * 
     * @param byStandardSystem 体系
     * @return 结果
     */
    @Override
    public int insertByStandardSystem(ByStandardSystem byStandardSystem)
    {
        byStandardSystem.setCreateTime(DateUtils.getNowDate());
        return byStandardSystemMapper.insertByStandardSystem(byStandardSystem);
    }

    /**
     * 修改体系
     * 
     * @param byStandardSystem 体系
     * @return 结果
     */
    @Override
    public int updateByStandardSystem(ByStandardSystem byStandardSystem)
    {
        return byStandardSystemMapper.updateByStandardSystem(byStandardSystem);
    }

    /**
     * 批量删除体系
     * 
     * @param ids 需要删除的体系主键
     * @return 结果
     */
    @Override
    public int deleteByStandardSystemByIds(Long[] ids)
    {
        return byStandardSystemMapper.deleteByStandardSystemByIds(ids);
    }

    /**
     * 删除体系信息
     * 
     * @param id 体系主键
     * @return 结果
     */
    @Override
    public int deleteByStandardSystemById(Long id)
    {
        return byStandardSystemMapper.deleteByStandardSystemById(id);
    }

    @Override
    public ByStandardSystem getAll(Long id) {
        //查出所有数据
        List<ByStandardSystem> list = byStandardSystemMapper.selectByStandardSystemList(null);
        //获取父级体系
        ByStandardSystem byStandardSystem = list.stream().filter((idy) -> idy.getId() == id).findFirst().get();
        //递归获取子集
        List<ByStandardSystem> listChildren = getChildren(list,byStandardSystem.getId());
        byStandardSystem.setChildren(listChildren);
        return byStandardSystem;
    }

    public List<ByStandardSystem> getChildren(List<ByStandardSystem> byStandardSystemList, Long parentId) {
        List<ByStandardSystem> children = new ArrayList();
        for(ByStandardSystem byStandardSystem : byStandardSystemList){
            if(byStandardSystem.getParentId() == parentId){
                ByStandardSystem byStandardSystem1 = new ByStandardSystem();
                BeanUtils.copyProperties(byStandardSystem,byStandardSystem1);
                List<ByStandardSystem> childrenList = getChildren(byStandardSystemList, byStandardSystem.getId());
                byStandardSystem1.setChildren(childrenList);
                children.add(byStandardSystem1);
            }
        }
        return children;
    }

}
